<template>
  <!-- 库存现有量 -->
  <div class="page-container">
    <div class="common-content-wrap">
      <TrTable
        v-model="searchParams"
        :btnList="btnList"
        :searchShowList="searchShowList"
        :searchHideList="searchHideList"
        :tableData="tableData"
        :tableColumn="tableColumn"
        :total="totalNum"
        requestName="searchHandle"
        @rowView="viewHandle($event)"
        @export="exportHandle"
      />
    </div>
    <TrDetail
      v-model="isShowDetailPopup"
      :leftList="detailList"
      statusModel="hidden"
      :detailData="detailData"
      :width="1227"
      :isNeedFooter="false"
    >
      <template #bomTable>
        <TrTable
          isPopup
          isNeedIndex
          v-model="detailSearchParams"
          :tableData="detailTableData"
          :total="detailTotal"
          :tableColumn="detailTableColumn"
          requestName="detailSearchHandle"
        />
      </template>
    </TrDetail>
  </div>
</template>

<script>
import { getList, getDetailList } from "@/api/report/inventoryOnHand.js";
import enumData from "@/utils/enumCode.js";

import TrTable from "@/components/Tr-table/Tr-table.vue";
import TrDetail from "@/components/Tr-detail/Tr-detail.vue";
export default {
  name: "workOrderManage",
  components: {
    TrTable,
    TrDetail,
  },
  data: () => ({
    detailSearchParams: {
      current: 1,
      size: 10,
      invTransId: "",
    },
    detailTotal: 0,
    detailTableColumn: [
      {
        type: "text",
        label: "条码标签",
        value: "barcode",
      },
      {
        type: "text",
        label: "入库单据",
        value: "billCode",
      },
      {
        type: "text",
        label: "仓库业务类型",
        value: "whBizTypeName",
      },
      {
        type: "text",
        label: "数量",
        value: "num",
      },
      {
        type: "text",
        label: "数量单位",
        value: "newNumUnit",
      },
      {
        type: "text",
        label: "重量",
        value: "weight",
      },
      {
        type: "text",
        label: "重量单位",
        value: "newWeightUnit",
      },
      {
        type: "text",
        label: "入库人员",
        value: "warehouseEntryUserName",
      },
      {
        type: "text",
        label: "入库时间",
        value: "warehouseEntryTime",
      },
      {
        type: "text",
        label: "审核人员",
        value: "auditUserName",
      },
      {
        type: "text",
        label: "审核时间",
        value: "auditTime",
      },
    ],
    detailTableData: [],
    isShowDetailPopup: false,
    detailList: [
      {
        type: "text",
        id: 1,
        label: "基础信息",
        list: [
          {
            text: "料号类型",
            id: "materialTypeName",
            width: 33.33,
          },
          {
            text: "物料料号",
            id: "materialCode",
            width: 33.33,
          },
          {
            text: "物料规格",
            id: "materialSpec",
            width: 33.33,
          },
          {
            text: "物料名称",
            id: "materialName",
            width: 33.3,
          },
          {
            text: "仓库编码",
            id: "warehouseCode",
            width: 33.33,
          },
          {
            text: "仓库名称",
            id: "warehouseName",
            width: 33.33,
          },
          {
            text: "库存数量",
            id: "invNum",
            width: 33.33,
          },
          {
            text: "数量单位",
            id: "newNumUnit",
            width: 33.33,
          },
          {
            text: "库存重量",
            id: "invWeight",
            width: 33.33,
          },
          {
            text: "重量单位",
            id: "newWeightUnit",
            width: 33.33,
          },
        ],
      },
      {
        type: "slot",
        slotName: "bomTable",
        label: "物料明细",
        slotBottom: 32,
      },
    ],
    detailData: {},
    btnList: [
      {
        label: "导出",
        option: "export",
        color: "blue",
      },
    ],
    totalNum: 0,
    tableData: [],
    searchParams: {
      current: 1,
      size: 10,
      // materialCodeType: "",
      materialCode: "",
      warehouseName: "",
      materialName: "",
      warehouseCode: "",
      materialIds: [],
    },
    searchShowList: [
      // {
      //   id: "materialCodeType",
      //   label: "料号类型",
      //   type: "select",
      //   multiple: true,
      //   isNeed: false,
      //   filterable: true,
      //   width: 33,
      //   list: [],
      //   value: [],
      // },
      {
        id: "materialCode",
        label: "物料料号",
        type: "loadselect",
        value: "",
        isShow: true,
        width: 33.3,
        currentErrorLabel: "请选择物料名称",
        errorStatus: false,
        selectedLabel: "",
        reviewLabel: "materialCode",
        defaultParams: { isEnabledList: [1] },
        requestObj: {
          url: "/api/mes-production-service/material/page",
          method: "post",
          searchKey: "materialCode",
          rlLabel: "materialCode",
          rlValue: "id",
          resultKey: "data,data,records",
        },
      },
      {
        id: "warehouseCode",
        label: "仓库编码",
        type: "input",
        width: 33.3,
        value: "",
      },
      {
        id: "warehouseName",
        label: "仓库名称",
        type: "input",
        width: 33.3,
        value: "",
      },
    ],
    searchHideList: [
      {
        id: "materialName",
        label: "物料名称",
        type: "loadselect",
        value: "",
        isShow: true,
        width: 33.3,
        currentErrorLabel: "请选择物料名称",
        errorStatus: false,
        selectedLabel: "",
        reviewLabel: "materialName",
        defaultParams: { isEnabledList: [1] },
        requestObj: {
          url: "/api/mes-production-service/material/page",
          method: "post",
          searchKey: "materialName",
          rlLabel: "materialName",
          rlValue: "id",
          resultKey: "data,data,records",
        },
      },
    ],
    tableColumn: [
      {
        type: "text",
        label: "料号类型",
        value: "materialTypeName",
      },
      {
        type: "text",
        label: "物料料号",
        value: "materialCode",
      },
      {
        type: "text",
        label: "物料名称",
        value: "materialName",
      },
      {
        type: "text",
        label: "物料规格",
        value: "materialSpec",
      },
      {
        type: "text",
        label: "仓库编码",
        value: "warehouseCode",
      },
      {
        type: "text",
        label: "仓库名称",
        value: "warehouseName",
      },
      {
        type: "text",
        label: "库存数量",
        value: "invNum",
      },
      {
        type: "text",
        label: "数量单位",
        value: "newNumUnit",
      },
      {
        type: "text",
        label: "库存重量",
        value: "invWeight",
      },
      {
        type: "text",
        label: "重量单位",
        value: "newWeightUnit",
      },
      {
        type: "option",
        label: "操作",
        fixed: "right",
        width: 80,
        showBtnList: [
          {
            label: "查看",
            option: "rowView",
          },
        ],
      },
    ],
  }),
  methods: {
    async getList() {
      this.getMaterialIds(this.searchParams);
      const { data: res } = await getList(this.searchParams);
      this.$validateData(res.data.records);
      this.tableData = res.data.records;
      this.tableData.forEach((item) => {
        item.newNumUnit = `${item.invNumUnitCode}(${item.invNumUnitName})`;
        item.newWeightUnit = `${item.invWeightUnitCode}(${item.invWeightUnitName})`;
      });
      this.totalNum = res.data.total;
    },
    getMaterialIds(searchParams) {
      searchParams.materialIds = [];
      const fieldsToCheck = ["materialCode", "materialName"];
      fieldsToCheck.forEach((field) => {
        const value = searchParams[field];
        if (value) {
          searchParams.materialIds.push(value);
        }
      });
    },
    async viewHandle(val) {
      console.log(val, ">>>>>>>>..");
      this.detailData = val;
      this.detailData.newNumUnit = `${val.invNumUnitCode}(${val.invNumUnitName})`;
      this.detailData.newWeightUnit = `${val.invWeightUnitCode}(${val.invWeightUnitName})`;
      await this.getDetailList();
      this.isShowDetailPopup = true;
    },
    searchHandle() {
      this.getList();
    },
    detailSearchHandle() {
      this.getDetailList();
    },
    async getDetailList() {
      this.detailSearchParams.invTransId = this.detailData.id;
      const { data: res } = await getDetailList(this.detailSearchParams);
      this.$validateData(res.data.records);
      this.detailTableData = res.data.records;
      this.detailTableData.forEach((item) => {
        item.newNumUnit = `${item.numUnitCode}(${item.numUnitName})`;
        item.newWeightUnit = `${item.weightUnitCode}(${item.weightUnitName})`;
      });
      this.detailTotal = res.data.total;
    },
    exportHandle() {
      this.$exportHandle({ vm: this, params: this.searchParams });
    },
    async getCodeTypeList() {
      let res = await this.$fetchEnumData([enumData.codeTypeList]);
      const findObj = this.$findByProperty(
        this.searchShowList,
        "id",
        "materialCodeType"
      );
      const list = res[enumData.codeTypeList];
      this.$set(findObj, "list", list);
    },
  },
  created() {
    // this.searchHandle();
    // this.getCodeTypeList();
  },
};
</script>
